#pragma once
#include <sys/epoll.h>
#include"log.hpp"
#include"common.hpp"
using namespace LogModule;
class Epoller
{
    const int size=1999;
public:
    Epoller()
    {   
        _efd=epoll_create(size);
        if(_efd<0)
        {
            LOG(LogLevel::DEBUG)<<"epoll_create errpor";
            exit(EPOLL_CREAT_ERR);
        }
        LOG(LogLevel::DEBUG)<<"epoll_create success!!! epoll_fd:"<<_efd;
    }
    void EpollInsertHelper(int fd,int op,uint32_t event)
    {
        struct epoll_event e;
        e.events=event;
        e.data.fd=fd;
        int n = epoll_ctl(_efd,op,fd,&e);
        if(n<0)
        {
            LOG(LogLevel::DEBUG)<<"加入epoll模型失败";
            exit(EPOLL_INSERT_ERR);
        }
        LOG(LogLevel::DEBUG)<<fd<<"已经加入epoll模型";
    }
    void EpollAdd(int fd,uint32_t event)
    {
        EpollInsertHelper(fd,EPOLL_CTL_ADD,event);
    }
    void EpollDel(int fd)
    {
        int n=epoll_ctl(_efd,EPOLL_CTL_DEL,fd,nullptr);
        (void) n;
    }
    void EpollMod(int fd,uint32_t event)
    {
        EpollInsertHelper(fd,EPOLL_CTL_MOD,event);
    }
    int EpollWaid(struct epoll_event*events,int size,int timeout )
    {
        int n=epoll_wait(_efd,events,size,timeout);
        if(n<0)
        {
            LOG(LogLevel::DEBUG)<<"epoll_wait err";

        }
        else if(n==0) 
        {
            LOG(LogLevel::DEBUG)<<"epoll_wait time out";

        }   
        else
        {
            LOG(LogLevel::DEBUG)<<"有"<<n<<"个事件就绪";

        }
        return n;
    }
    ~Epoller(){}

private:
    int _efd = -1;
};